const token = require('./token'); //token

//模型
const UserModel = require("../models/user"); //用户

module.exports = {

    //根据用户权限、取值长度参数组装范围值
    async region(ctx, len, next) {
        let region = false; //定义用户权限
        try {
            let webToken = String(ctx.header.authorization); //获取当前登录用户权限
            if (webToken) {
                let tokenVerify = await token.verify(webToken); //验签
                let user = await UserModel.findOne({
                    '_id': String(tokenVerify.id)
                }); //获取用户信息
                //获取用户对应的权限值【主要是去掉权限值右侧多余的0，为后面范围值的设定铺平道路】
                let regionArr = String(user.region).split('');
                for (let i = 0; i < 1; i--) {
                    if (regionArr[regionArr.length - 1] == "0") {
                        regionArr.splice(-1, 1);
                        continue;
                    } else {
                        break;
                    };
                };
                //根据传递过来的取值长度，为最小值补全0，为最大值补全9
                let cutRegion = regionArr.join('');
                if (cutRegion.length < len) {
                    let min = '',
                        max = '',
                        cutLen = len - cutRegion.length;
                    for (let i = 0; i < cutLen; i++) {
                        min += '0';
                        max += '9';
                    };
                    let newRegin = {};
                    newRegin.$gt = Number(cutRegion + min);
                    newRegin.$lt = Number(cutRegion + max);
                    region = newRegin;
                };
            }
        } catch (e) {
            region = false;
        } finally {
            return region;
        }
    },




};